/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Loan_Magazine_Panel.java
 *
 * Created on Mar 24, 2010, 8:19:49 PM
 */


package library.GUI;

import javax.swing.JOptionPane;
import javax.swing.table.*;
import java.util.*;

import SourceCode.*;

/**
 *
 * @author zyang
 */
public class Loan_Magazine_Panel extends javax.swing.JPanel {

    DefaultTableModel defTableModel;
    DefaultTableModel tableModelLoans;
    int copyid = 0;
    /** Creates new form Loan_Magazine_Panel */
    public Loan_Magazine_Panel() {
        initComponents();
         defTableModel = new DefaultTableModel();
        this.tblMagazineTable.setModel(defTableModel);
        defTableModel.addColumn("CopyID");
        defTableModel.addColumn("Mangazine Name");
        defTableModel.addColumn("Issure Number");
        defTableModel.addColumn("Checkout Length");
        defTableModel.addColumn("Available");

        tableModelLoans = new DefaultTableModel();
        this.jTable1.setModel(tableModelLoans);
        tableModelLoans.addColumn("Member ID");
        tableModelLoans.addColumn("Member Name");
        tableModelLoans.addColumn("Copy ID");
        tableModelLoans.addColumn("Copy Item Name");
        tableModelLoans.addColumn("Checkout Date");
        tableModelLoans.addColumn("Return Date");
    }

    public void clearTableData(DefaultTableModel model)
    {
       while (model.getRowCount()>0){
        model.removeRow(0);
       }
    }

     public void LoadMangazineDataToTable() {
        Object[] rowdata;
            for(Magazine magazine : LibraryGeneral.S_Magazines)
            {
                for(Copy copy : magazine.Copies)
                {
                    int copyid = copy.getCopyid();
                    String magazineTitle = magazine.getTitle();
                    String magazineIssureNo = magazine.getIssueno();
                    int CheckoutLength = magazine.getCheckoutlength();
                    boolean isAvailable = copy.IsAvailable();
                    rowdata = new Object[]{copyid, magazineTitle, magazineIssureNo, CheckoutLength, isAvailable};
                    defTableModel.addRow(rowdata);
                }
            }
    }

    public void LoadMemberLoanData(int MemberID)
    {
        Member member = LibraryGeneral.Map_Members.get(MemberID);
        Object[] rowdata;
        if(member == null)
        {
            JOptionPane.showMessageDialog(null, "This member does not exist");
            return;
        }
        if(member.loans.size() == 0)
        {
            JOptionPane.showMessageDialog(null, "This member does not have loans");
            return;
        }
        else
        {
            for(Loan loan : member.loans)
            {
                String memberName = loan.getMember().getNAme();
                int Copyid = loan.getCopy().getCopyid();
                String CopyItemName = loan.getCopy().getItem().getTitle();
                Date checkoutDate = loan.getCheckoutDate();
                Date returnDate = loan.getReturnDate();
                rowdata = new Object[]{MemberID, memberName, Copyid, CopyItemName, checkoutDate, returnDate};
                tableModelLoans.addRow(rowdata);
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtMagazineTitle = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtIssueNumber = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtCheckoutLength = new javax.swing.JTextField();
        btnMagazineBorrow = new javax.swing.JButton();
        btnMagazineReturn = new javax.swing.JButton();
        btnMagazineSearch = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        txtMemberID = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblMagazineTable = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        txtCopyID = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        btnListMemberLoans = new javax.swing.JButton();

        jLabel1.setText("Magazine Title:");

        jLabel2.setText("Issue No.");

        jLabel3.setText("Checkout Length:");

        btnMagazineBorrow.setText("Borrow");
        btnMagazineBorrow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMagazineBorrowActionPerformed(evt);
            }
        });

        btnMagazineReturn.setText("Return");
        btnMagazineReturn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMagazineReturnActionPerformed(evt);
            }
        });

        btnMagazineSearch.setText("Search");
        btnMagazineSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMagazineSearchActionPerformed(evt);
            }
        });

        jLabel4.setText("Member ID:");

        tblMagazineTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "CopyID", "Managazine Title", "Issure Number", "Checkout Length"
            }
        ));
        tblMagazineTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblMagazineTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblMagazineTable);

        jLabel5.setText("Copy ID:");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Member ID", "Member Name", "Copy ID ", "Copy Item Name", "Checkout Date", "Return Date"
            }
        ));
        jScrollPane2.setViewportView(jTable1);

        btnListMemberLoans.setText("List Loans of Member");
        btnListMemberLoans.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnListMemberLoansActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnListMemberLoans)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnMagazineReturn))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 479, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(18, 18, 18)
                                .addComponent(txtMemberID))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtMagazineTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtIssueNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtCopyID)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCheckoutLength, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnMagazineBorrow, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnMagazineSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 479, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(txtIssueNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtMagazineTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(txtCheckoutLength, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtMemberID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(txtCopyID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnMagazineBorrow)
                    .addComponent(btnMagazineSearch))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnListMemberLoans)
                    .addComponent(btnMagazineReturn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnMagazineSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMagazineSearchActionPerformed
        // TODO add your handling code here:
        if(this.txtCopyID.getText().equals("") || this.txtMagazineTitle.getText().equals(""))
        {
            this.LoadMangazineDataToTable();
        }
    }//GEN-LAST:event_btnMagazineSearchActionPerformed

    private void btnMagazineBorrowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMagazineBorrowActionPerformed
        // TODO add your handling code here:
        if(defTableModel.getRowCount() == 0)
        {
            JOptionPane.showMessageDialog(null, "No copys list now.");
        }
        if(this.txtMemberID.getText().equals("") || copyid == 0)
        {
            JOptionPane.showMessageDialog(null, "could not borrow without Memeber id or Copyid");
        }
        //check the member exist
        int memberId = Integer.parseInt(this.txtMemberID.getText());
        Member member = LibraryGeneral.Map_Members.get(memberId);
        if(member == null)
        {
            JOptionPane.showMessageDialog(null, "The member id is not exists, you could not borrow.");
            return;
        }

        //Make loan
        Copy copy = LibraryGeneral.Map_Copies.get(copyid);
        //copy.setAvailable(false);

        if(copy.IsAvailable())
        {
             DataRange dateRange = new DataRange(new Date(), copy.getItem().getCheckoutlength());
             Loan loan = new Loan(copy, member, dateRange.getStart());
             member.loans.add(loan);
             JOptionPane.showConfirmDialog(null, "Are you sure to borrow?");
        }
        else
        {
            JOptionPane.showMessageDialog(null, "This copy is not available now, it could be reserved for you");
            member.reserveItem(copy.getItem());
        }
       


    }//GEN-LAST:event_btnMagazineBorrowActionPerformed

    private void tblMagazineTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblMagazineTableMouseClicked
        // TODO add your handling code here:
        int selectedMagazineRow = this.tblMagazineTable.getSelectedRow();
        Object copyidObj = this.tblMagazineTable.getValueAt(selectedMagazineRow, 0);
        copyid =  Integer.parseInt(copyidObj.toString());
        this.txtCopyID.setText(copyidObj.toString());
    }//GEN-LAST:event_tblMagazineTableMouseClicked

    private void btnListMemberLoansActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnListMemberLoansActionPerformed
        // TODO add your handling code here:
        if(this.txtMemberID.getText().equals(""))
        {
            JOptionPane.showMessageDialog(null, "You need input MemberID");
        }
        int memberId = Integer.parseInt(this.txtMemberID.getText());
        clearTableData(tableModelLoans);
        this.LoadMemberLoanData(memberId);
    }//GEN-LAST:event_btnListMemberLoansActionPerformed

    private void btnMagazineReturnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMagazineReturnActionPerformed
        // TODO add your handling code here:
        if(this.jTable1.getRowCount() == 0)
        {
            JOptionPane.showMessageDialog(null, "Please Choose a loan of specific member");
            return;
        }
        if(this.txtMemberID.getText().equals(""))
        {
            JOptionPane.showMessageDialog(null, "Please Choose a member who want to return");
            return;
        }

        int rowSelected = this.jTable1.getSelectedRow();
        Object o = this.jTable1.getValueAt(rowSelected, 2);
        Copy copy = LibraryGeneral.Map_Copies.get(Integer.parseInt(o.toString()));
         int memberId = Integer.parseInt(this.txtMemberID.getText());
         Member member = LibraryGeneral.Map_Members.get(memberId);
         member.returncopy(copy);
         //LibraryGeneral.
         JOptionPane.showConfirmDialog(null, "Return book?");
    }//GEN-LAST:event_btnMagazineReturnActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnListMemberLoans;
    private javax.swing.JButton btnMagazineBorrow;
    private javax.swing.JButton btnMagazineReturn;
    private javax.swing.JButton btnMagazineSearch;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable tblMagazineTable;
    private javax.swing.JTextField txtCheckoutLength;
    private javax.swing.JTextField txtCopyID;
    private javax.swing.JTextField txtIssueNumber;
    private javax.swing.JTextField txtMagazineTitle;
    private javax.swing.JTextField txtMemberID;
    // End of variables declaration//GEN-END:variables

}
